<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2017-03-19
 * Time: 15:37
 */

namespace app\src\jobs;


use app\src\sunsun\filterVat\action\FilterVatDeviceEventAction;
use app\src\sunsun\filterVat\action\TcpLogAction;
use app\src\system\action\ApiCallHisClearOldAction;
use think\Exception;
use think\queue\Job;

/**
 * Class ClearDeviceEventData
 * 1天执行一次
 * @package app\src\jobs
 */
class ClearDeviceEventData
{

    /**
     * 清理 设备事件表
     * @param Job $job
     * @param $data
     */
    public function fire(Job $job,$data){
        addLog("ClearDeviceEventData/fire",'开始执行',$data,"[队列任务]");
        $attempts = $job->attempts() > 0 ? $job->attempts() : 1;
        if($attempts > 3){
            $job->delete();
            return;
        }
        $interval = $attempts * 60;
        $now = time();
        try{

            //1. 设备事件日志60天内保留
            $dataTimestamp = $now  - 60*24*3600;
            $result = (new FilterVatDeviceEventAction())->clearExpiredData($dataTimestamp);

            //1. tcp接口日志7天内保留
            $dataTimestamp = $now - 7*24*3600;
            $result = (new TcpLogAction())->clearExpiredData($dataTimestamp);

            //1. 接口日志7天内保留
            $dataTimestamp = $now - 7*24*3600;
            $result = (new ApiCallHisClearOldAction())->clearExpiredData($dataTimestamp);

            $job->delete();

        }catch (Exception $exception){
            addLog("ClearDeviceEventData/fire",'异常',$exception->getTraceAsString(),"[队列任务]");
            $job->release($interval);
        }finally{
        }
    }
}